package com.linuxone.news.yahoo;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.HeadingTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.ParagraphTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.NodeVisitor;

import com.linuxone.news.NewsThread;
import com.linuxone.news.server.TodayNewsParser;


public class YahooNewsParser implements TodayNewsParser<YahooTodayNews> {

	String baseUrl = "http://hk.news.yahoo.com";
	String url = baseUrl + "/hongkong/index.html";
	
	@Override
	public YahooTodayNews parse() {

		final YahooTodayNews todayNews = new YahooTodayNews();		
		final List<String> links = new ArrayList<String>();
		
		Parser parser;
		
		try {
			parser = new Parser();
			parser.setURL(url);
			parser.setEncoding(parser.getEncoding());
			parser.visitAllNodesWith(new NodeVisitor() {

				@Override
				public void visitTag(Tag tag) {
					if ("div".equalsIgnoreCase(tag.getTagName()) && tag.getAttribute("class")!=null && "com-img-news".equals(tag.getAttribute("class"))) {
						NodeList nl = new NodeList();
						tag.collectInto(nl, new TagNameFilter("a"));
						Node[] linkNodes = nl.toNodeArray();
						for (Node n : linkNodes) {
							if (n instanceof LinkTag) {
								links.add(baseUrl + ((LinkTag) n).getAttribute("href"));
							}
						}
					}
					
					if ("div".equalsIgnoreCase(tag.getTagName()) && tag.getAttribute("id")!=null && "article-listing-more".equals(tag.getAttribute("id"))) {
						NodeList nl = new NodeList();
						tag.collectInto(nl, new TagNameFilter("a"));
						Node[] linkNodes = nl.toNodeArray();
						for (Node n : linkNodes) {
							if (n instanceof LinkTag) {
								links.add(baseUrl + ((LinkTag) n).getAttribute("href"));
							}
						}
					}
				}

			});
			// handle all link
//			System.out.println("link size:"+links.size());
			for (final String link : links) {
				parser = new Parser(link);
				parser.setEncoding(parser.getEncoding());
				parser.visitAllNodesWith(new NodeVisitor() {
					public void visitTag(Tag tag) {
						NewsThread newsThread = new NewsThread();
						if ("div".equalsIgnoreCase(tag.getTagName()) && tag.getAttribute("id")!=null && "article-content".equals(tag.getAttribute("id"))) {
							NodeList nl = new NodeList();
							// title
							tag.collectInto(nl, new TagNameFilter("h1"));
							Node[] titleNodes = nl.toNodeArray();
							for(Node n : titleNodes)
								if(n instanceof HeadingTag)
									newsThread.setTitle(n.toPlainTextString());
							// date
							tag.collectInto(nl, new TagNameFilter("div"));
							Node[] dateNodes = nl.toNodeArray();
							for(Node n : dateNodes){
								if(n instanceof Div && ((Div) n).getAttribute("class")!=null && "info".equals(((Div) n).getAttribute("class"))){
									String Datetime = n.toPlainTextString();
									// regexp
								    Pattern pattern = Pattern.compile("\\(.+\\)([\\d]{4})年([\\d]+)月([\\d]+)日 .+ ([\\d]{1,2}):([\\d]{1,2})");
								    Matcher matcher = pattern.matcher(Datetime);
								    boolean matchFound = matcher.find();
								    String year="";
								    String month="";
								    String day="";
								    String hour="";
								    String minute="";
								    try {
									    while(matchFound) {
									    	year = matcher.group(1);
									    	month = matcher.group(2);
									    	day = matcher.group(3);
									    	hour = matcher.group(4);
									    	minute = matcher.group(5);
									    	
									    	if(matcher.end() + 1 <= Datetime.length()) {
									    		matchFound = matcher.find(matcher.end());
									    	}else{
									    		break;
									    	}
									    }
									} catch (NumberFormatException e) {
											e.printStackTrace();
									}
									//newsThread.setDate(year+"-"+month+"-"+day+" "+hour+":"+minute+":00", "yyyy-M-d HH:mm:ss");
									try {
										// 
										SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
										formatter.setTimeZone(TimeZone.getTimeZone("GMT+8"));
										newsThread.setDate(formatter.parse(year+"-"+month+"-"+day+" "+hour+":"+minute+":00"));
									} catch (ParseException e) {
										e.printStackTrace();
									}
									newsThread.setDateTime(year+"-"+month+"-"+day+" "+hour+":"+minute);
									//newsThread.setDatetime(n.toPlainTextString());
								}
							}
							// content
							List<String> myContentList = new ArrayList<String>();
							tag.collectInto(nl, new TagNameFilter("p"));
							Node[] contentNodes = nl.toNodeArray();
							for(Node n : contentNodes){
								if(n instanceof ParagraphTag) 
									//newsContent += n.toPlainTextString() + "\n";
									myContentList.add(n.toPlainTextString());
							}
							newsThread.setContent(myContentList);
							newsThread.setURL(link);
							newsThread.setId(UUID.randomUUID().toString());
							todayNews.addNewsThread(newsThread);
						}
					}
				});
			}
						
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return todayNews;
	}
	
}
